import numpy as np
from PIL import ImageGrab
import cv2
import time

while(True):
    screen = np.array(ImageGrab.grab(bbox=(0,0,953,850)))
    last_time = time.time()

    gray = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)
    cap = cv2.CascadeClassifier(
        "C:/Users/Administrator/AppData/Local/Programs/Python/Python38-32/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml"
    )
    faceRects = cap.detectMultiScale(
        gray, scaleFactor=1.2, minNeighbors=3, minSize=(3, 3))
    if len(faceRects):
        for faceRect in faceRects:
            x, y, w, h = faceRect
            cv2.rectangle(screen, (x, y), (x + w,y + h), (0, 255, 0), 2)  # 框出人脸
    # cv2.imshow("Image", screen)
    cv2.imshow('window', cv2.cvtColor(screen,cv2.COLOR_BGR2RGB))

    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break